package com.zt.weibo.utils;

import java.util.ArrayList;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import com.zt.weibo.vo.CommentInfo;
import com.zt.weibo.vo.StatusInfo;
import com.zt.weibo.vo.UserInfo;


/**
 * 该类用于解析JSON字符串
 * @author zhaotong
 */
public class JsonAnalyze
{
	private static JsonAnalyze instance = null;
	private ArrayList<StatusInfo> mStatusInfoList = null;
	private ArrayList<UserInfo> mUserInfoList = null;
	private ArrayList<CommentInfo> mCommentInfoList = null;
	private EnumInfoType eCurInfoType = EnumInfoType.E_INFO_TYPE_NONE;
	
	/**
	 * 微博信息标志位枚举类
	 * @author zhaotong
	 */
	private enum EnumInfoType
	{
		E_INFO_TYPE_NONE,
		E_INFO_TYPE_STATUS,
		E_INFO_TYPE_COMMENT,
		E_INFO_TYPE_USER
	}
	
	public static JsonAnalyze getInstance()
	{
		if(instance == null)
		{
			instance = new JsonAnalyze();
		}
		return instance;
	}
	
	/**
	 * 根据传入的JSON字符串，进行解析并返回存有相应对象的列表（如微博信息列表、用户信息列表 、评论列表）
	 * @param strJson 由新浪API返回得到的JSON字符串
	 * @return 获取转发、评论、新微薄时应转为{@code ArrayList}({@link StatusInfo})；
	 * <p>获取用户信息时应转为{@code ArrayList}({@link UserInfo})</p>
	 */
	public ArrayList<?> getInfoListFromJson(String strJson)
	{
		eCurInfoType = EnumInfoType.E_INFO_TYPE_NONE;
		try
		{
			JSONObject mJsonObject = new JSONObject(strJson);
			//微博列表
			if(mJsonObject.optJSONArray("statuses") != null)
			{
				eCurInfoType = EnumInfoType.E_INFO_TYPE_STATUS;
				JSONArray mJsonArray = mJsonObject.optJSONArray("statuses");
				mStatusInfoList = getStatusInfoList(mJsonArray);
			}
			//评论列表
			else if (mJsonObject.optJSONArray("comments") != null)
			{
				eCurInfoType = EnumInfoType.E_INFO_TYPE_COMMENT;
				JSONArray mJsonArray = mJsonObject.optJSONArray("comments");
				ArrayList<CommentInfo> tempCommentInfos = new ArrayList<CommentInfo>();
				for(int iIndex = 0; iIndex < mJsonArray.length(); iIndex ++)
				{
					CommentInfo tempCommentInfo = new CommentInfo();
					JSONObject tempObject = mJsonArray.optJSONObject(iIndex);
					tempCommentInfo = analyzeCommentInfo(tempObject);
					tempCommentInfos.add(tempCommentInfo);
				}
				mCommentInfoList = tempCommentInfos;
			}
			//用户列表
			else if (mJsonObject.optJSONArray("users") != null)
			{
				eCurInfoType = EnumInfoType.E_INFO_TYPE_USER;
				JSONArray mJsonArray = mJsonObject.getJSONArray("users");
				ArrayList<UserInfo> tempUserInfoList = new ArrayList<UserInfo>();
				for(int iIndex = 0; iIndex < mJsonArray.length(); iIndex ++)
				{
					UserInfo tempUserInfo = new UserInfo();
					JSONObject tempObject = mJsonArray.optJSONObject(iIndex);
					tempUserInfo = analyzeUserInfo(tempObject);
					tempUserInfoList.add(tempUserInfo);
				}
				mUserInfoList = tempUserInfoList;
			}
		}
		catch (JSONException e)
		{
			e.printStackTrace();
		}
		switch(eCurInfoType)
		{
		case E_INFO_TYPE_STATUS:
			return mStatusInfoList;
		case E_INFO_TYPE_COMMENT:
			return mCommentInfoList;
		case E_INFO_TYPE_USER:
			return mUserInfoList;
		case E_INFO_TYPE_NONE:
			return null;
		}
		return null;
	}
	
	/**
	 * 获取存有微博信息的列表
	 * @param mJsonArray “statuses”JSON数组
	 * @return {@link StatusInfo}列表
	 * @throws JSONException 
	 */
	private ArrayList<StatusInfo> getStatusInfoList(JSONArray mJsonArray) throws JSONException
	{
		ArrayList<StatusInfo> tempStatusInfoList = new ArrayList<StatusInfo>();
		if(0 == mJsonArray.length())
		{
			return null;
		}
		for(int iIndex = 0; iIndex < mJsonArray.length(); iIndex ++)
		{
			JSONObject tempObject = mJsonArray.optJSONObject(iIndex);
			//加入信息列表
			tempStatusInfoList.add(iIndex, analyzeStatusInfo(tempObject));
			//如果有转发则开始解析被转发的微博
			if(tempObject.optJSONObject("retweeted_status") != null)
			{
				tempObject = tempObject.getJSONObject("retweeted_status");
				tempStatusInfoList.get(iIndex).setRetweetInfo(analyzeStatusInfo(tempObject));
			}
		}
		return tempStatusInfoList;
	}
	
	/**
	 * 解析JSON对象中“Status”对象信息
	 * @param tempObject
	 * @param iIndex
	 * @return {@link StatusInfo}
	 */
	public StatusInfo analyzeStatusInfo(JSONObject tempObject)
	{
		StatusInfo mStatusInfo = new StatusInfo();
		//解析该条微博的信息
		try
		{
			mStatusInfo.strId = tempObject.optString("id");
			if(1 == tempObject.optInt("deleted"))
			{
				mStatusInfo.iDeleted = 1;
				return mStatusInfo;
			}
			mStatusInfo.strCreatTime = FormatString.formatCreatTime(tempObject.optString("created_at"));
			mStatusInfo.strContent = tempObject.optString("text");
			mStatusInfo.strSource = FormatString.formatSource(tempObject.optString("source"));
			mStatusInfo.bIsFavorit = tempObject.optBoolean("favorited");
			mStatusInfo.bIsTruncat = tempObject.optBoolean("truncated");
			mStatusInfo.in_reply_to_status_id = tempObject.optString("in_reply_to_status_id");
			mStatusInfo.in_reply_to_user_id = tempObject.optString("in_reply_to_user_id");
			mStatusInfo.in_reply_to_screen_name = tempObject.optString("in_reply_to_screen_name");
			mStatusInfo.strThumbnailPic = tempObject.optString("thumbnail_pic");
			mStatusInfo.strBmiddlePic = tempObject.optString("bmiddle_pic");
			mStatusInfo.strOriginalPic = tempObject.optString("original_pic");
			mStatusInfo.strGeo = tempObject.optString("geo");
			mStatusInfo.iRepostsCount = tempObject.optInt("reposts_count");
			mStatusInfo.iCommentsCount = tempObject.optInt("comments_count");
			//解析发表微博的用户的信息
			JSONObject tempUserObject = tempObject.optJSONObject("user");
			mStatusInfo.mUserInfo = analyzeUserInfo(tempUserObject);
		}
		catch (Exception e) 
		{
			e.printStackTrace();
		}
		return mStatusInfo;
	}
	
	/**
	 * 解析JSON对象中的“user”对象信息
	 * @param tempUserObject
	 * @return {@link UserInfo}
	 * @throws JSONException
	 */
	public UserInfo analyzeUserInfo(JSONObject tempUserObject) throws JSONException
	{
		UserInfo mUserInfo = new UserInfo();
		mUserInfo.strId = tempUserObject.optString("id");
		mUserInfo.strCreatTime = FormatString.formatCreatTime(tempUserObject.optString("created_at"));
		mUserInfo.strName = tempUserObject.optString("screen_name");
		mUserInfo.strLocation = tempUserObject.optString("location");
		mUserInfo.strDescription = tempUserObject.optString("description");
		mUserInfo.strUrl = tempUserObject.optString("url");
		mUserInfo.strProfileImageUrl = tempUserObject.optString("profile_image_url");
		mUserInfo.strAvatarLargeImageUrl = tempUserObject.optString("avatar_large");
		mUserInfo.strGender = tempUserObject.optString("gender");
		mUserInfo.iFollowersCount = tempUserObject.optInt("followers_count");
		mUserInfo.iFriendsCount = tempUserObject.optInt("friends_count");
		mUserInfo.iStatusesCount = tempUserObject.optInt("statuses_count");
		mUserInfo.iFavouritesCount = tempUserObject.optInt("favourites_count");
		mUserInfo.bIsFollowing = tempUserObject.optBoolean("following");
		mUserInfo.bIsFollowMe = tempUserObject.optBoolean("follow_me");
		mUserInfo.strRemark = tempUserObject.optString("remark");
		mUserInfo.bIsVerified = tempUserObject.optBoolean("verified");
		mUserInfo.strVerifiedReason = tempUserObject.optString("verified_reason");
		return mUserInfo;
	}
	
	/**
	 * 解析JSON中的“comment”信息
	 * @param tempObject 
	 * @return {@link CommentInfo}
	 * @throws JSONException
	 */
	public CommentInfo analyzeCommentInfo(JSONObject tempObject) throws JSONException
	{
		CommentInfo mCommentInfo = new CommentInfo();
		mCommentInfo.strId = tempObject.optString("id");
		mCommentInfo.strCreatTime = FormatString.formatCreatTime(tempObject.optString("created_at"));
		mCommentInfo.strContent = tempObject.optString("text");
		mCommentInfo.strSource = FormatString.formatSource(tempObject.optString("source"));
		//解析评论的作者信息
		JSONObject tempUserObject = tempObject.optJSONObject("user");
		mCommentInfo.mUserInfo = analyzeUserInfo(tempUserObject);
		//解析被评论的微博信息
		JSONObject tempStatusObject = tempObject.optJSONObject("status");
		mCommentInfo.mStatusBeenCommented = analyzeStatusInfo(tempStatusObject);
		return mCommentInfo;
	}
	
}
